package org.eclipse.epsilon.emc.spreadsheets.csv.instantiate;

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.eclipse.epsilon.common.util.FileUtil;
import org.eclipse.epsilon.emc.spreadsheets.SpreadsheetRow;
import org.eclipse.epsilon.emc.spreadsheets.csv.CsvModel;
import org.eclipse.epsilon.emc.spreadsheets.csv.CsvRow;
import org.eclipse.epsilon.emc.spreadsheets.csv.CsvWorksheet;
import org.eclipse.epsilon.eol.EolModule;
import org.eclipse.epsilon.eol.exceptions.models.EolModelElementTypeNotFoundException;
import org.junit.BeforeClass;
import org.junit.Test;

public class NoHeaderCreateInstanceTest
{
	private static CsvModel model = null;
	private static String filePath = "resources/CsvFile.txt";

	@BeforeClass
	public static void setUpFileAndLoad() throws Exception
	{
		FileUtil.setFileContents("", new File(filePath));
		setUp();
	}

	private static void setUp() throws Exception
	{
		model = new CsvModel();
		model.setFile(filePath);
		model.setConfigurationFile("resources/CsvFileTestConfig.xml");
		model.setHasHeader(false);
		model.setStoredOnDisposal(false);
		model.setName("MODEL");
		model.load();
	}

	@Test
	public void createRow() throws EolModelElementTypeNotFoundException
	{
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("c_0", "value0");
		values.put("c_1", "value1");
		values.put("c_2", "value2");
		values.put("c_3", "value3");
		values.put("c_4", "value4");
		values.put("c_5", "value5");
		model.createInstance(CsvWorksheet.NAME, values);

		List<SpreadsheetRow> rows = (List<SpreadsheetRow>) model.getAllOfKind(CsvWorksheet.NAME);
		assertTrue(CollectionUtils.isNotEmpty(rows));
		assertTrue(rows.size() == 1);
		CsvRow row = (CsvRow) rows.get(0);
		CsvWorksheet worksheet = (CsvWorksheet) model.getWorksheetByType(CsvWorksheet.NAME);
		assertTrue(row.getVisibleCellValue(worksheet.getColumn("col0")).equals("value0"));
		assertTrue(row.getVisibleCellValue(worksheet.getColumn("col1")).equals("value1"));
		assertTrue(row.getVisibleCellValue(worksheet.getColumn("col2")).equals("value2"));
		assertTrue(row.getVisibleCellValue(worksheet.getColumn("col3")).equals("value3"));
		assertTrue(row.getVisibleCellValue(worksheet.getColumn("col4")).equals("value4"));
		assertTrue(row.getVisibleCellValue(worksheet.getColumn("col5")).equals("value5"));
	}

	@Test
	public void createRowWithEOL() throws Exception
	{
		final EolModule module = new EolModule();
		module.parse("return new CSV(Map{'col0'='v0', 'col4'='v4'});");
		module.getContext().getModelRepository().addModel(model);
		Object result = module.execute();

		CsvRow row = (CsvRow) result;
		CsvWorksheet worksheet = (CsvWorksheet) model.getWorksheetByType(CsvWorksheet.NAME);
		assertTrue(row.getVisibleCellValue(worksheet.getColumn("col0")).equals("v0"));
		assertTrue(row.getVisibleCellValue(worksheet.getColumn("col1")).equals(""));
		assertTrue(row.getVisibleCellValue(worksheet.getColumn("col2")).equals(""));
		assertTrue(row.getVisibleCellValue(worksheet.getColumn("col3")).equals(""));
		assertTrue(row.getVisibleCellValue(worksheet.getColumn("col4")).equals("v4"));
		assertTrue(row.getVisibleCellValue(worksheet.getColumn("col5")).equals(""));
	}

}
